<?php
namespace app\admin\controller;
use think\Db;
use think\Session;
use think\Request;
class Order  extends Common
{
        public function index(){
            return $this->view->fetch();
        }
        public function allorders(){

        	$key=input('key');
        	if($key){
        		//$where['key']=array('like','%'.$key.'%');//模糊搜索
        		$where['key']=$key;
        	}
        	//数量
	        $leaveCount = Db::name("order")->where($where)->count();

	        //分页数据
	        $limit=input('limit');
	        $page=input('page');
	        $start=($page-1)*$limit;

	        //调取数据
	        $orderall=Db::name("order")->where($where)->limit($start,$limit)->order('id desc')->select();

	        foreach ($orderall as $k => $v) {
	        	if($v['state']=='0'){
	        		$info='<span style="color:orange;font-weight: bold;">已使用</span>';
	        	}else if($v['state']=='p0'){
	        		$info='<span style="color:#f00;font-weight: bold;">未付款</span>';
	        	}else if($v['state']=='1'){
	        		$info='<span style="color:green;font-weight: bold;">未使用</span>';
	        	}else{
	        		$info='异常';
	        	}

	        	$orderall[$k]['state_l']=$info;
	        	$user=get_user($v['userid']);
	        	$orderall[$k]['user']=base64_decode($user['username']);
	        }

	        //API数据格式处理
	        $data= array();
	        $data['code']=0;
	        $data['msg']='';
	        $data['count']=$leaveCount;
	        $data['data']=$orderall;

	        //返回json数据
	        echo $data=json_encode($data);
        }
        public function editorder(){
        	$id=input('id');
	        $info=Db::name('order')->where(array('id' =>$id))->find();
	        $user=get_user($info['userid']);
	        if($user){
		        $info['username']=base64_decode($user['username']);
		        $info['head']=$user['head'];
		    }else{
		    	$info['username']='不存在';
		        $info['head']='';
		    }

		    if($info['type']=='1'){
		    	$info['type']='代金卷';
		    }else if($info['type']=='2'){
		    	$info['type']='礼品卷';
		    }else if($info['type']=='3'){
		    	$info['type']='积分商品';
		    }

		    if($info['state']=='0'){
		    	$info['state_n']='已使用';
		    }else if($info['state']=='1'){
		    	$info['state_n']='未使用';
		    }else if($info['state']=='p0'){
		    	$info['state_n']='未付款';
		    }
		        $this->view->assign('info', $info);
				return $this->view->fetch();
	    }
	    public function editordersave()
	    {
	    	$data=input('data');
	    	$data=json_decode($data,true);
	    	$where['id']=$data['id'];
	    	$save['state']=$data['state'];
	    	$info=Db::name('order')->where($where)->update($save);

	    	$user=SESSION::get('cmuser');
	        $log_title=$data['id'];
	        $log="edit order ' $log_title ' save";
	        set_log($user,$log);

	    	if($info){
	    		return 1;
	    	}else{
	    		return 99;
	    	}
	    }
	    public function check()
	    {
	    	$key=input('key');
	    	$this->view->assign('key',$key);
	    	if($key){
	    		$where['key']=$key;
	    		$order=Db::name('order')->where($where)->find();
	    	}
	    	//dump($order);
	    	$user=get_user($order['userid']);
	    	$order['user']=$user['wxname'];

	    	if($order['type']==1){
	    		$order['type_l']='代金卷';
	    	}else if($order['type']==2){
	    		$order['type_l']='礼品卷';
	    	}else if($order['type']==3){
	    		$order['type_l']='积分商品';
	    	}

	    	if($order['state']=='p0'){
	    		$order['state_l']='<span style="color:#f00">未付款</span>';
	    	}else if($order['state']=='1'){
	    		$order['state_l']='<span style="color:green">未使用</span>';
	    	}else if($order['state']=='0'){
	    		$order['state_l']='<span style="color:orange">已使用</span>';
	    	}

	    	if($order['type']==1){
	    		$order['pay_l']='￥'.$order['pay'].'元';
	    	}else if($order['type']==2){
	    		$order['pay_l']='￥'.$order['pay'].'元';
	    	}else if($order['type']==3){
	    		$order['pay_l']=$order['pay'].'积分';
	    	}

	    	$this->view->assign('order',$order);
	    	return $this->view->fetch();
	    }
	    public function check_state()
	    {
	    	$where['id']=input('id');
	    	$save['state']=0;
	    	$save['checkmaster']=SESSION::get('cmuser');
	    	$save['checktime']=date('Y-m-d H:i:s',time());
	    	$info=Db::name('order')->where($where)->update($save);
	    	$order=Db::name('order')->where($where)->find();

	    	if($info){
	    		//写入日志
	    		$user=SESSION::get('cmuser');
		        $log_title=$where['id'];
		        $log="$user  check order ' $log_title ' state";
		        set_log($user,$log);

		        $user=Db::name('user')->where('id',$order['userid'])->find();
		        if($user){
			        //发送消息模板
			        $user['wxuser']=base64_decode($user['username']);
			        $postdata = array(
					            'first' => array(
					                'value' => '尊敬的'.$user['wxuser'].'，您的卡卷已使用',
					                'color' => '#FF0000'
					            ),
					            'keyword1' => array(
					                'value' => $order['key'],
					                'color' => '#000'
					            ),
					            'keyword2' => array(
					                'value' => $order['shoptitle'],
					                'color' => '#000'
					            ),
					            'keyword3' => array(
					                'value' => date('Y-m-d H:i:s',time()),
					                'color' => '#000'
					            ),
					            'keyword4' => array(
					                'value' => '陕西西北金行有限责任公司',
					                'color' => '#000'
					            ),
					            'remark' => array(
					                'value' => '如有任何疑问，请立即通过官方微信反馈',
					                'color' => '#000'
					            )
					        );
					$wxurl='http://www.sxjhol.vip/index.php/index/user/show?id='.$where['id'];
					send_wx($postdata,$user['openid'],'DT1pWKlEf-yQ5s1mh0wYWkR2oppLx4TiBXfDF3_iT1w',$wxurl);
					
		        }
		     

	    		$this->success('核销成功',url('order/check'));
	    	}else{
	    		$this->error('核销失败','order/check');
	    	}
	    }
	    public function checked()
	    {
	    	$username=input('username');
	    	$adminDb=Db::name('admin');
	    	$admin=$adminDb->select();
	    	$this->view->assign('admin',$admin);
	    	$this->view->assign('username',$username);
	    	$master=SESSION::get('cmuser');

	    	$userCheck=$adminDb->where('master',$master)->find();
	    	$this->view->assign('userCheck',$userCheck);
	    	
	    	return $this->view->fetch();
	    }
	    public function myorder()
	    {
        	$master=SESSION::get('cmuser');
        	$order=Db::name('order');
        	$adminDb=Db::name('admin');
        	$admin=$adminDb->where('master',$master)->find();

        	if($admin['ini'] != 1){
        		//$where['key']=array('like','%'.$key.'%');//模糊搜索
        		$where['checkmaster']=$master;
        	}else{
        		$username=input('username');
        		if($username){
	            	$where['checkmaster']=$username;
	        	}
        	}
        	//数量
	        $leaveCount = $order->where($where)->count();

	        //分页数据
	        $limit=input('limit');
	        $page=input('page');
	        $start=($page-1)*$limit;

	        //调取数据
	        $orderall=$order->where($where)->limit($start,$limit)->order('checktime desc')->select();

	        foreach ($orderall as $k => $v) {
	        	if($v['state']=='0'){
	        		$info='<span style="color:orange;font-weight: bold;">已使用</span>';
	        	}else if($v['state']=='p0'){
	        		$info='<span style="color:#f00;font-weight: bold;">未付款</span>';
	        	}else if($v['state']=='1'){
	        		$info='<span style="color:green;font-weight: bold;">未使用</span>';
	        	}else{
	        		$info='异常';
	        	}

	        	$orderall[$k]['state_l']=$info;
	        	$user=get_user($v['userid']);
	        	$orderall[$k]['user']=base64_decode($user['username']);
	        }
	        //API数据格式处理
	        $data= array();
	        $data['code']=0;
	        $data['msg']='';
	        $data['count']=$leaveCount;
	        $data['data']=$orderall;

	        //返回json数据
	        echo $data=json_encode($data);
	    }
    

}
